package com.itzyf.pokemondata.activity

import android.view.inputmethod.EditorInfo
import android.widget.TextView
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.BaseViewHolder
import com.itzyf.pokemondata.R
import com.itzyf.pokemondata.entity.HuntingGround
import com.itzyf.pokemondata.entity.PageVo
import com.itzyf.pokemondata.entity.ResultVo
import com.itzyf.pokemondata.service.PokemonService
import com.itzyf.pokemondata.utils.OnRefreshListener
import com.itzyf.pokemondata.utils.RetrofitUtils
import com.itzyf.pokemondata.utils.setOnRefreshListener
import com.tydic.baselibrary.activity.BaseActivity
import kotlinx.android.synthetic.main.activity_list.*
import retrofit2.Call
import retrofit2.Response
import java.text.SimpleDateFormat
import java.util.*

class HuntingGroundActivity : BaseActivity() {
    var mDatas: MutableList<HuntingGround?> = ArrayList()
    var index = 0
    val sdf = SimpleDateFormat("yyyy-MM-dd", Locale.CHINESE)

    private lateinit var mAdapter: QuickAdapter

    override fun getContentViewId() = R.layout.activity_list

    override fun getToolbarTitle() = "高级狩猎场"

    override fun handlerActivity() {

        mAdapter = QuickAdapter(R.layout.item_hunting_ground, mDatas)
        rv_list.layoutManager = androidx.recyclerview.widget.LinearLayoutManager(this)
        rv_list.adapter = mAdapter
        showProgressDialog()
        queryDatas()

        mAdapter.setOnLoadMoreListener({ queryDatas(index++) }, rv_list)

        pull_to_refresh.setOnRefreshListener(object : OnRefreshListener {
            override fun onRefresh() {
                index = 0
                queryDatas(index)
            }
        })

        mBtnSearch.setOnClickListener {
            val query = mSearchView.text.toString()
            queryDatas(query = query)
        }
        mSearchView.setOnEditorActionListener(TextView.OnEditorActionListener { _, actionId, _ ->
            if (actionId == EditorInfo.IME_ACTION_SEARCH) {
                val query = mSearchView.text.toString()
                queryDatas(query = query)
                return@OnEditorActionListener true
            }
            false
        })


    }

    fun queryDatas(index: Int = 0, pageSize: Int = 20, query: String = "") {
        showProgressDialog()
        RetrofitUtils.getInstance()
            .getApiService(PokemonService::class.java)
            .queryListHg(index + 1, pageSize, query)
            .enqueue(object : retrofit2.Callback<ResultVo<PageVo<HuntingGround>>> {
                override fun onFailure(call: Call<ResultVo<PageVo<HuntingGround>>>, t: Throwable) {
                    dismissProgressDialog()
                }

                override fun onResponse(
                    call: Call<ResultVo<PageVo<HuntingGround>>>,
                    response: Response<ResultVo<PageVo<HuntingGround>>>
                ) {
                    dismissProgressDialog()
                    val body = response.body()
                    if (body!!.code == 200) {
                        if (index == 0) {
                            mDatas.clear()
                            pull_to_refresh.finishRefresh()
                        }
                        mDatas.addAll(body.result.records)

                        if (body.result.pages <= index + 1) {
                            mAdapter.loadMoreEnd()
                        } else {
                            mAdapter.loadMoreComplete()
                        }
                        mAdapter.notifyDataSetChanged()
                    }

                }
            })
    }

    internal inner class QuickAdapter(layoutResId: Int, data: List<HuntingGround?>) :
        BaseQuickAdapter<HuntingGround, BaseViewHolder>(layoutResId, data) {

        override fun convert(helper: BaseViewHolder, item: HuntingGround?) {
            helper.setText(R.id.tvStartDate, sdf.format(item?.startDate))

            helper.setText(R.id.tvQuantity1, " ${item?.quantity1}")
            helper.setText(R.id.tvQuantity2, " ${item?.quantity2}")
            helper.setText(R.id.tvPokemon1, item?.pokemon1)
            helper.setText(R.id.tvPokemon2, item?.pokemon2)
        }
    }
}
